Skip to content

perf: presize node-set maps in mapNodes, ClosestNodes and winnowNodes#563

Merged
mna merged 2 commits into
PuerkitoBio:masterfrom
jvoisin:precise
May 28, 2026
Merged

perf: presize node-set maps in mapNodes, ClosestNodes and winnowNodes#563
mna merged 2 commits into
PuerkitoBio:masterfrom
jvoisin:precise

Conversation

@jvoisin
Copy link
Copy Markdown
Contributor

@jvoisin jvoisin commented May 27, 2026

Three internal helpers build a map[*html.Node]bool from a known set of nodes (or use one as a dedup accumulator over a known number of input nodes) but allocate the map without a size hint. They then incur grow-and-double rehashing as entries are inserted.

Pass len(nodes) as the size hint at make time:

  • traversal.go mapNodes: dedup map shared by appendWithoutDuplicates across every per-node result. mapNodes is the engine behind most traversal helpers (Parents, Children, Next/Prev, Siblings, Find*, Contents, ...), so the impact is broad.
  • traversal.go ClosestNodes: target-node set built from nodes...
  • filter.go winnowNodes: large-N path's lookup set built from nodes...

Double-digit performance gains across ~all benchmarks.

Three internal helpers build a map[*html.Node]bool from a known set of
nodes (or use one as a dedup accumulator over a known number of input
nodes) but allocate the map without a size hint. They then incur
grow-and-double rehashing as entries are inserted.

Pass len(nodes) as the size hint at make time:

- traversal.go mapNodes: dedup map shared by appendWithoutDuplicates
  across every per-node result. mapNodes is the engine behind most
  traversal helpers (Parents, Children, Next/Prev, Siblings, Find*,
  Contents, ...), so the impact is broad.
- traversal.go ClosestNodes: target-node set built from nodes...
- filter.go winnowNodes: large-N path's lookup set built from nodes...

Double-digit performance gains across ~all benchmarks.
@mna mna merged commit 7a46fb4 into PuerkitoBio:master May 28, 2026
6 checks passed
@mna
Copy link
Copy Markdown
Member

mna commented May 28, 2026

Thanks for the batch of PRs, love those small, focused improvements! Let me know if you have more coming in, once you're done with them I'll cut a new release.

@jvoisin
Copy link
Copy Markdown
Contributor Author

jvoisin commented May 28, 2026

Thank you for your patience reviewing my late-night profiler-infused pull-requests :)

I'll keep sending them until there are no more low-hanging fruits, so likely at least until the end of next week

@jvoisin jvoisin deleted the precise branch May 28, 2026 22:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants